package com.wcy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.annotation.aop.PreAuth;
import com.wcy.entity.LearningResource;
import com.wcy.entity.ResourceCategory;
import com.wcy.entity.queryVo.QueryConvertor;
import com.wcy.service.IResourceCategoryService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-27
 */
@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {
    @Resource
    private IResourceCategoryService resourceCategoryService;

    @PreAuth("CATEGORY:QUERY")
    @GetMapping("/page")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Page<ResourceCategory>> getPage(@RequestParam(defaultValue = "1") int pageNum,
                                                          @RequestParam(defaultValue = "10") int pageSize,
                                                          HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        Page<ResourceCategory> page = resourceCategoryService.page(new Page<>(pageNum, pageSize),wrapper);
        return ResponseEntity.ok(page);
    }

    @GetMapping("/list")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<List<ResourceCategory>> getList(HttpServletRequest request) {
        String queryData = request.getHeader("X-Query-Data");
        QueryWrapper wrapper = new QueryConvertor(queryData).convert(true);
        List<ResourceCategory> list = resourceCategoryService.list(wrapper);
        return ResponseEntity.ok(list);
    }

    @PreAuth("CATEGORY:ADD")
    @PostMapping("/add")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> add(@RequestBody ResourceCategory resourceCategory) {
        resourceCategoryService.save(resourceCategory);
        return ResponseEntity.ok().build();
    }

    @PreAuth("CATEGORY:ALTER")
    @PutMapping("/update")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> update(@RequestBody ResourceCategory resourceCategory) {
        resourceCategoryService.updateById(resourceCategory);
        return ResponseEntity.ok().build();
    }

    @PreAuth("CATEGORY:DELETE")
    @DeleteMapping("/delete/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<Void> delete(@PathVariable Integer id) {
        resourceCategoryService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/get/{id}")
    @SentinelResource(value = "#root.targetClass + '.' + #root.methodName")
    public ResponseEntity<ResourceCategory> get(@PathVariable("id") Integer id) {
        ResourceCategory resourceCategory = resourceCategoryService.getById(id);
        return ResponseEntity.ok(resourceCategory);
    }
}
